﻿///<reference path="/Scripts/jquery-1.9.1-vsdoc.js" />

$.fn.kendoMenuOpenFixup = function (e) {
	var evt = e;
}

$.fn.PrepareLayoutMenus = function () {
	$("#layoutMenu").kendoMenu({
		open: $.fn.kendoMenuOpenFixup
	});
	$("#layoutMenu").removeClass("notDisplayed");
}

//------------------Ouvre la dialogBox----------------
$.fn.jqDialog = function (idDiv, titre, redirection, width, height) {
	if (width == null) {
		width = 600
	}
	if (height == null) {
		height = 400
	}
	var dialogBox = $(idDiv);
	$(dialogBox).hide();
	$(dialogBox).dialog({
		title: titre,
		autoOpen: false,
		resizable: false,
		modal: true,
		minHeight: height,
		minWidth: width,
		buttons: [
			{
				text: "Ok",
				click: function () {
					if (redirection != null) {
						if (redirection != "") {
							$.fn.NavigateTo(redirection);
						}
					}
					$(this).dialog("close");
				}
			}
		],
		open: function (event, ui) { }
	});
};
$.fn.inlinePopup = function (url, title, width, height) {
	var IP = $("#inlinePopup");
	var IFR = $("#ifrInlinePopup");
	IFR.attr("src", url);
	IP.attr("title", title);
	IP.dialog({
		height: height,
		width: width,
		modal: true,
		resizable: false,
		closeOnEscape: true
	});
}

//------------------Ouvre la dialogBox----------------
$.fn.jqDialogFunction = function (idDiv, titre, okFunction, cancelFunction, openFunction) {
	var dialogBox = $(idDiv)
	//$(dialogBox).removeClass("notDisplayed");
	if (cancelFunction == null) {
		cancelFunction = function () {
			$(this).dialog("close");
		}
	}
	$(dialogBox).hide();
	$(dialogBox).dialog({
		title: titre,
		autoOpen: false,
		resizable: false,
		modal: true,
		minHeight: 450,
		minWidth: 800,
		open: openFunction,
		buttons: [
			{
				text: "Ok",
				click: okFunction
			}
			,
			{
				text: "Cancel",
				click: cancelFunction
			}
		]
	});
	var form = dialogBox.find("form");
	if (form != null) {
		$.validator.unobtrusive.parse(form);
	}
	//$(".ui*").mouseout(function (e) { e.preventDefault()});
	$(idDiv).dialog('open');
}
//------------------Appel l'ouverture de la dialogBox----------------
$.fn.jqDialogConfirm = function (title, message, confirmButtonTitle, cancelButtonTitle, okFunction, height, width) {
	var dialogBox = $('#dialogMessage');
	dialogBox.text(message);
	$(dialogBox).hide();
	$(dialogBox).dialog({
		title: title,
		autoOpen: false,
		resizable: false,
		modal: true,
		minHeight: height,
		minWidth: width,
		buttons: [
			{
				text: confirmButtonTitle,
				click: okFunction
			}
			,
			{
				text: cancelButtonTitle,
				click: function () {
					$(this).dialog("close");
				}
			}
		]
	});
	dialogBox.dialog('open');
}
//------------------Yes No Dialog box----------------
$.fn.jqDialogYesNoFunction = function (title, message, yesButtonTitle, noButtonTitle, yesFunction, noFunction, openFunction, width, height) {
	var dialogBox = $('#dialogMessage');
	dialogBox.text(message);
	if (width == null) {
		width = 800;
	}
	if (height == null) {
		height = 450;
	}
	$(dialogBox).hide();
	$(dialogBox).dialog({
		title: title,
		autoOpen: false,
		resizable: false,
		modal: true,
		width: width,
		height: height,
		open: openFunction,
		closeOnEscape: false,
		buttons: [
			{
				text: yesButtonTitle,
				click: yesFunction
			}
			,
			{
				text: noButtonTitle,
				click: noFunction
			}
		]
	});
	var form = dialogBox.find("form");
	if (form != null) {
		$.validator.unobtrusive.parse(form);
	}
	//$(".ui*").mouseout(function (e) { e.preventDefault()});
	dialogBox.dialog('open');
}
//------------------Appel l'ouverture de la dialogBox----------------
$.fn.jqDialogMessageOpen = function () {
	$.fn.jqDialog('#dialogMessage', 'Alerte Erreur');
	$('#dialogMessage').dialog('open');
}
//------------------Appel l'ouverture de la dialogBox----------------
$.fn.jqDialogAlert = function (title, message, redirection, width, height) {
	$.fn.jqDialog('#dialogMessage', title, redirection, width, height);
	$('#dialogMessage').html('');
	$('#dialogMessage').text(message);
	$('#dialogMessage').dialog('open');
}
//------------------Appel l'ouverture de la dialogBox----------------
$.fn.jqDialogAjaxError = function (title, message, redirection, width, height) {
	$.fn.jqDialog('#dialogMessage', title, redirection, width, height, false);
	var iFrame = $("#dialogMessage").find(".ui-dialog-content").find("#AjaxErrorIFrame");
	$('#dialogMessage').html('');
	if (iFrame.length == 0) {
		iFrame = $("<iframe id='AjaxErrorIFrame' name='AjaxErrorIFrame' frameborder='0' scrolling='yes' width='570' height='270'>");
		iFrameParent = $("#dialogMessage");
		iFrameParent.append(iFrame);
	}
	var doc = ($("#AjaxErrorIFrame")[0]).contentWindow;
	doc.document.open();
	doc.document.write(message);
	window.document.close();
	$('#dialogMessage').dialog('open');
}
//------------------Appel l'ouverture de la dialogBox pour une erreur----------------
$.fn.jqDialogErreur = function (divErreur, exception) {
	$.fn.jqDialog("#dialogErreur", "Alerte Pop-Up");
	$('.ui-dialog-title').fadeIn().text('Alerte Erreur');
	$('.ui-dialog-content').fadeIn().text('Message d\'erreur : ' + exception);
};
//------------------Appel de la fonction jqDialogErreur en lui passant le nom du div----------------
$.fn.jqDialogErreurOpen = function (exception) {
	var divErreur = '#GestionErreur';
	$.fn.jqDialogErreur(divErreur, exception);
	$(divErreur).dialog('open');
};
//------------------Serialise les objets d'une form----------------
$.fn.serializeObject = function () {
	var obj = {};
	$(":input[type='checkbox']").each(function (e) {
		obj[this.name] = this.checked;
	});
	var o = {};
	var a = this.serializeArray();
	$.each(a, function () {
		if (o[this.name] !== undefined) {
			if (obj !== undefined) {
				if (obj[this.name] !== undefined) {
					o[this.name] = obj[this.name];
				}
			}
			else {
				if (!o[this.name].push) {
					o[this.name] = [o[this.name]];
				}
				o[this.name].push(this.value || '');
			}
		}
		else {
			o[this.name] = this.value || '';
		}
	});
	return o;
};
//------------------Navigate to the url----------------
$.fn.NavigateTo = function (url) {
	window.location.href = url;
}

//------------------Localisation du Datepicker----------------
jQuery(function ($) {
	if ($.datepicker != null) {
		$.datepicker.regional['fr'] = {
			clearText: 'Effacer', clearStatus: '',
			closeText: 'Fermer', closeStatus: 'Fermer sans modifier',
			prevText: '<Préc', prevStatus: 'Voir le mois précédent',
			nextText: 'Suiv>', nextStatus: 'Voir le mois suivant',
			currentText: 'Courant', currentStatus: 'Voir le mois courant',
			monthNames: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin',
		'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'],
			monthNamesShort: ['Jan', 'Fév', 'Mar', 'Avr', 'Mai', 'Jun',
		'Jul', 'Aoû', 'Sep', 'Oct', 'Nov', 'Déc'],
			monthStatus: 'Voir un autre mois', yearStatus: 'Voir un autre année',
			weekHeader: 'Sm', weekStatus: '',
			dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
			dayNamesShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
			dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
			dayStatus: 'Utiliser DD comme premier jour de la semaine', dateStatus: 'Choisir le DD, MM d',
			dateFormat: 'dd/mm/yy', firstDay: 0,
			initStatus: 'Choisir la date', isRTL: false
		};
		$.datepicker.setDefaults($.datepicker.regional['fr']);
	}
});

$.fn.TabifyCodeFiles = function () {
	$(".divCodeFilesTabStrip").kendoTabStrip({ theme: "Blue Opal" });
}

$.fn.MainAjaxFailure = function (a, b, c) {
	if (a.scriptToExecute != null) {
		if (a.scriptToExecute != "") {
			eval(a.scriptToExecute);
		}
	} else {
		$.fn.jqDialogAjaxError(b + " " + a.status + ":" + a.statusText, a.responseText, "", 600, 400);
	}
}

$.validator.methods.range = function (value, element, param) {
	var globalizedValue = value.replace(",", ".");
	return this.optional(element) || (globalizedValue >= param[0] && globalizedValue <= param[1]);
}
$.validator.methods.number = function (value, element) {
	return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
}
$.fn.ensureStopPropagation = function (evt) {
	evt.preventDefault();
	evt.stopPropagation();
	evt.stopImmediatePropagation();
}
